package com.fangzhi;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fangzhi.mobel.ProductType;
import com.fangzhi.service.ProductTypeService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@SpringBootTest
public class ReadExcel {

    @Autowired
    private ProductTypeService productTypeService;

    private Set<String> productSet = new HashSet<>();
    @Test
    public void start(){
        try {
            //创建工作簿对象
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("/Users/edy/yhqx_data/dddd.xlsx"));
            //获取工作簿下sheet的个数
            int sheetNum = xssfWorkbook.getNumberOfSheets();
            System.out.println("该excel文件中总共有："+sheetNum+"个sheet");
            //遍历工作簿中的所有数据
            for(int i = 0;i<sheetNum;i++) {
                //读取第i个工作表
                System.out.println("读取第"+(i+1)+"个sheet");
                XSSFSheet sheet = xssfWorkbook.getSheetAt(i);
                //获取最后一行的num，即总行数。此处从0开始
                int maxRow = sheet.getLastRowNum();
                for (int row = 0; row <= maxRow; row++) {
                    XSSFRow row1 = sheet.getRow(row);
                    System.out.println(String.valueOf(row1.getCell(i)).trim());
                    productSet.add(String.valueOf(row1.getCell(i)).trim());
                }
                removeProduct(productSet);
                for (int row = 0; row <= maxRow; row++) {
                    XSSFRow row1 = sheet.getRow(row);
                    //获取最后单元格num，即总单元格数 ***注意：此处从1开始计数***
                    joinn(row1);
//                    System.out.println("--------第" + row + "行的数据如下--------");
//                    for (int rol = 0; rol < maxRol; rol++){
//                        if (rol == 0){
//                            ProductType one = productTypeService.getOne(Wrappers.<ProductType>lambdaQuery().eq(ProductType:get));
//                        }
//                    }
//                    System.out.println();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void removeProduct(Set<String> productList){
        List<ProductType> list = productTypeService.list(Wrappers.<ProductType>lambdaQuery().in(ProductType::getProductName , productList));
        for (ProductType productType : list) {
            System.out.println(productType.getProductName());
            List<ProductType> list1 = productTypeService.list(Wrappers.<ProductType>lambdaQuery().eq(ProductType::getParentId, productType.getId()));
            for (ProductType type : list1) {
                List<ProductType> list2 = productTypeService.list(Wrappers.<ProductType>lambdaQuery().eq(ProductType::getParentId, type.getId()));
                for (ProductType productType1 : list2) {
                    productTypeService.removeById(productType1.getId());
                }
                productTypeService.removeById(type.getId());
            }
            productTypeService.removeById(productType.getId());
        }
    }
    
    public void joinn(XSSFRow row){
        int maxRol = row.getLastCellNum();
        Long parenntId = 1L;
        for (int i = 0; i < maxRol - 1; i++) {
            ProductType one = productTypeService.getOne(Wrappers.<ProductType>lambdaQuery()
                    .eq(ProductType::getProductName, String.valueOf(row.getCell(i)).trim()).eq(ProductType::getParentId, parenntId));
            if (null == one){
                if (String.valueOf(row.getCell(i)).trim().replace(" ", "").equals("null"))
                    break;
                one = new ProductType().setProductName(String.valueOf(row.getCell(i)).trim()).setParentId(parenntId).setStatus(1).setLevel( i + 1);
            }else{
                one.setLevel(i + 1);
            }

            if (i == 2){
                System.out.println(one.getProductName());
//                XSSFCell cell = row.getCell(i + 1);
//                String s = String.valueOf(cell);
//                double number = Double.parseDouble(s);
//                NumberFormat nf = NumberFormat.getNumberInstance();
//                nf.setRoundingMode(RoundingMode.UP);
//                System.out.println(number);
//                String str = nf.format(number * 100D);
//                int i1 = Integer.parseInt(str);
//                System.out.println(str + "---" + i1);
//                Long round = Math.round(number * 100);
                XSSFCell cell = row.getCell(i + 1);
                String s = String.valueOf(cell);
                System.out.println(s.replace("\n", ""));
                BigDecimal bd = new BigDecimal(s.replace("\n", ""));
                // 价格保留一位小数
                bd = bd.setScale(1, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100));
//                bd = bd.multiply(BigDecimal.valueOf(100));
                Double result = bd.setScale(0, BigDecimal.ROUND_DOWN).doubleValue();
                System.out.println(s +"+++++"+result.intValue());
                one.setPrice(result.intValue());
            }
            productTypeService.saveOrUpdate(one);
            parenntId = one.getId();
        }
    }

    public static void main(String[] args) {
        String s = "0.155";
        BigDecimal bd = new BigDecimal(s.replace("\n", ""));
        bd = bd.multiply(BigDecimal.valueOf(100));
        Double result = bd.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(result);
    }
}
